package com.hospital.dept.yuan.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.github.pagehelper.PageInfo;
import com.hospital.dept.yuan.entity.Department;
import com.hospital.dept.yuan.entity.Department_CT;
import com.hospital.dept.yuan.service.CtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/dept")
public class CtController {

    @Autowired
    private CtService ctService;

    @RequestMapping("/ctPage")  //分页查询
    public String pageSelect(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "") String cuserName, Integer cdiagnoseNO, Model model) {
        PageInfo<Department_CT> pageInfo = ctService.CtPaging(pageNum, cuserName, cdiagnoseNO);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String now = df.format(new Date());  //new Date()为获取当前系统时间
        model.addAttribute("nowDate", now);  //保存当前时间
        model.addAttribute("pageInfo", pageInfo);  //当前分页
        if (!cuserName.equals("")) {  //如果查询带条件则翻页需条件
            model.addAttribute("cuserName", cuserName);
        }
        if (cdiagnoseNO!=null){
            model.addAttribute("cdiagnoseNO",cdiagnoseNO);
        }
        return "dept/chaCt";
    }

    @RequestMapping("/select")  //分页查询
    public String select(Model model,Integer userId, String userName,String userType, @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum) {

        //查询未照片
        PageInfo<Department> deptOne = ctService.ctListOne(userId,userName,userType, pageNum);
        model.addAttribute("pageOne", deptOne);
        model.addAttribute("userName", userName);

        //查询已照片
        PageInfo<Department> deptTwo = ctService.ctListTwo(userName, pageNum);
        //当前时间
        SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateTime = date.format(new Date());
        model.addAttribute("pageTwo", deptTwo);
        model.addAttribute("dateTime", dateTime);
        return "dept/department";
    }

    @ResponseBody
    @RequestMapping("/updateId")  //按照编号修改状态
    public String upDateId(Integer id, Integer state) {
        boolean modify = ctService.modify(id, state);
        return JSON.toJSONString(modify);  //返回布尔
    }

    @ResponseBody
    @RequestMapping("/insertCt")  //新增ct
    public String newCT(Department_CT ct) {
        boolean b = ctService.addCt(ct);
        return JSON.toJSONString(b);
    }

    //新增ct记录
    @RequestMapping("/insert")
    @ResponseBody
    public String insert(String ctNum, String ctName, String ctTime, String part, String situation, String doctorId) {
        Department_CT ct = new Department_CT(ctNum, ctName, ctTime, part, situation, doctorId);
        boolean b = ctService.insertCt(ct);
        return JSON.toJSONString(b);
    }

    @RequestMapping("/radiology")
    public String radiology(Model model,Integer id,@RequestParam(defaultValue = "") String userName,@RequestParam(defaultValue = "") String userType, @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum){
        PageInfo<Department> deptOne = ctService.ctListOne(id,userName,userType, pageNum);
        model.addAttribute("pageOne", deptOne);

        if (!userName.equals("")) {  //如果查询带条件则翻页需条件
            model.addAttribute("userName", userName);
        }
        if (id!=null){
            model.addAttribute("id",id);
        }
        if (!userType.equals("")){
            model.addAttribute("userType",userType);
        }
        return "dept/radiology";
    }

    @RequestMapping("/insertCT")
    @ResponseBody
    public String insertCT(String cdiagnoseNO, String cuserName, String checkType, String inspectionItem, String diagnose, String doctorid,String userTime) {
        Department_CT ct = new Department_CT(cdiagnoseNO, cuserName, checkType, inspectionItem, diagnose, doctorid);
        boolean b = ctService.insertCT(ct,userTime);
        return JSON.toJSONString(b);
    }

    @RequestMapping("/lszd")  //历史诊断记录
    @ResponseBody
    public String lszd(Integer cdiagnoseNO) {
        return JSONArray.toJSONString(ctService.hiszd(cdiagnoseNO));
    }

}
